
class Solution {
    public int[] getSumAbsoluteDifferences(int[] nums) {
        int[] ans = new int[nums.length];
         //先计算后缀和
        int next = 0;
        for (int i = nums.length-1;i>=0;i--){
            ans[i] =  next;
            next = next+nums[i];
        }
        int pre = 0; //记录前缀和 可以减少一个数组
        for (int i = 0;i<nums.length;i++){
            ans[i] = (nums[i]*i-pre)+(ans[i]-nums[i]*(nums.length-i-1));
            pre = pre + nums[i];
        }
        return ans;
    }
}

